/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/

package org.jfab.core.factory;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.jfab.core.collection.CGAnnotationList;
import org.jfab.core.collection.CGParameterList;
import org.jfab.core.collection.CGTypeList;
import org.jfab.core.collection.CommentList;
import org.jfab.core.collection.ModifierList;
import org.jfab.core.collection.ParameterNameList;
import org.jfab.core.model.CGParameter;
import org.jfab.core.model.CGType;

/**
 * Provides a parameter factory.
 */
public final class DefaultCGParameterFactory implements CGParameterFactory
{
    @Override
    public final CGParameter createParameter(final CGType parameterType,
            final String parameterName)
    {
        final CGTypeList referencedTypes = null;
        final CommentList commentLines = null;
        final CGAnnotationList annotations = null;
        final ModifierList modifiers = null;

        return createParameter(referencedTypes, commentLines, annotations,
                modifiers, parameterType, parameterName);
    }

    @Override
    public final CGParameter createParameter(final CGTypeList referencedTypes,
            final CommentList commentLines, final CGAnnotationList annotations,
            final ModifierList modifiers, final CGType parameterType,
            final String parameterName)
    {
        Validate.notNull(parameterType, "parameterType is empty");
        Validate.notEmpty(parameterName, "parameterName is empty");

        final CGParameter answer = new CGParameter();

        if (referencedTypes != null)
        {
            answer.getReferencedTypes().addAll(referencedTypes);
        }

        if (commentLines != null)
        {
            answer.getCommentLines().addAll(commentLines);
        }

        if (annotations != null)
        {
            answer.getAnnotations().addAll(annotations);
        }

        if (modifiers != null)
        {
            answer.getModifiers().addAll(modifiers);
        }

        answer.setType(parameterType);
        answer.setName(parameterName);

        return answer;
    }

    @Override
    public final CGParameterList createParameters(
            final CGTypeList parameterTypes,
            final ParameterNameList parametersNames)
    {
        Validate.notNull(parameterTypes, "parameterTypes is null");
        Validate.notNull(parameterTypes, "parameterTypes is null");
        Validate.isTrue((parameterTypes.size() == parametersNames.size())
                || parametersNames.isEmpty(),
                "Parameter types and names size mismatch.");

        final CGParameterList answer = new CGParameterList();

        final int size = parameterTypes.size();

        for (int i = 0; i < size; i++)
        {
            final CGType parameterType = parameterTypes.get(i);
            String parameterName = null;

            if (i < parametersNames.size())
            {
                parameterName = parametersNames.get(i);
            }

            if (StringUtils.isEmpty(parameterName))
            {
                parameterName = "arg" + i;
            }

            final CGParameter cgParameter = createParameter(parameterType,
                    parameterName);
            answer.add(cgParameter);
        }

        return answer;
    }
}
